/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cmc.rra.controller;

import com.cmc.rra.forms.SignInForm;
import com.cmc.rra.service.AppUserService;
import com.cmc.rra.service.impl.AppUserServiceImpl;
import com.cmc.rra.sub.AppUser;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.WebRequest;

/**
 *
 * @author CMC
 */
@Controller
@RequestMapping("/sign-in")
public class SignInController {
    
    AppUserService appUserService = null;
    AppUser appUser = null;
   
    @RequestMapping(method = RequestMethod.GET)
    public String showSignInForm(@ModelAttribute SignInForm signInForm) {
        return "/rraHome";
    }
    
    @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    public String doSigIn(@ModelAttribute SignInForm signInForm,
            WebRequest request, HttpServletResponse response, HttpServletRequest req) {
        String userId = signInForm.getUserId().trim();
        String password = signInForm.getPassword().trim();
        
        appUserService = new AppUserServiceImpl();
        appUser = appUserService.authenticate(userId, password);
        //System.out.println("================AppUser"+appUser);
        if (appUser == null) {
            request.setAttribute("dbError", "Invalid UserId/Password",
                    WebRequest.SCOPE_SESSION);
            return "Invalid UserName/Password";
        }
        
        return "login";
        
    }
    
    
}
